package junit;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateTest {

	private static SessionFactory sessionFactory=null;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		Configuration configuration=new Configuration();
		configuration.configure();
		sessionFactory=configuration.buildSessionFactory();
	}
	@Test
	public void ServiceTest(){
		Session session=sessionFactory.openSession();
		
		String sql="select o.productid,count(o) from OrderItem o where o.order.createDate between to_date(?,'yyyy-mm-dd') and to_date(?,'yyyy-mm-dd') group by o.productid";
		// select count(o),book from orderItem o where o.order.createDate between ? and ? group by o.book 
		Query query=session.createQuery(sql);
		Date date=new Date();
		DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=Calendar.getInstance();
		calendar.set(2012, 5-1, 22);
		String dateStr=dateFormat.format(calendar.getTime());
		query.setString(0, "2010-11-1");
		query.setString(1,dateStr);
		
		List<Object[]> list=query.list();
		for (Object[] objects : list) {
				for (Object object : objects) {
					System.out.print("=:="+object);
				}
				System.out.println(" ====");
		}
		session.close();
		sessionFactory.close();
		System.out.println("ok");
		System.out.println(dateStr);
	}
	public static void main(String[] args) {
		Date date=new Date();
		DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		System.out.println(dateFormat.format(date));
		
		Calendar calendar=Calendar.getInstance();
		calendar.set(2011, 4-1, 7);
		System.out.println(dateFormat.format(calendar.getTime()));
	}
}
